package cn.wolfcode.luowowo.mgrsite.TimedTask;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Set;

/**
 * @author 猫教授
 * @date 2019/8/14 22:29
 */
//@Component
public class RedisDataPersistenceJob {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    //秒 分 小时 月份中的日期 月份 星期中的日期 年份
    //0 0 0 1/1 * ?
    @Scheduled(cron="0/10 * * * * ?")
    public void redisDataPersistence(){
        //我们需要把redis的数据同步到数据库
        //首先我们需要拿出所有的keys 键值集合,然后进行遍历
        //查询出来的每一个对象都去进行同步复制数据到攻略对象
        //然后再保存攻略对象
        System.out.println("我同步了");
        String prefix= "STRATEGY_STATIC_VO*";
        Set<String> set=strategyStatisRedisService.getKeyList(prefix);
        StrategyDetail detail = new StrategyDetail();
        set.forEach((key)->{
            StrategyStatisVO vo = strategyStatisRedisService.getVoByRedisKey(key);
            try {
                BeanUtils.copyProperties(detail,vo);
                detail.setId(vo.getStrategyId());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            strategyDetailService.synchronizationStrategyStatis(detail);
        });














//
//        String keyPattern = StrategyStatisKeyPrefix.STRATEGY_STATIS_VO.getPrefix() + ":*";
//        List<StrategyStatisVO> list = strategyStatisRedisService.getStatisVOs(keyPattern);
//        for (StrategyStatisVO vo : list) {
//            strategyDetailService.updateStatis(vo);
//        }
    }
}
